package com.xunge.controller;

import com.xunge.anno.Log;
import com.xunge.pojo.Homework;
import com.xunge.pojo.HomeworkQueryParam;
import com.xunge.pojo.PageResult;
import com.xunge.pojo.Result;
import com.xunge.service.HomeworkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/homework")
@RestController
public class HomeworkController {
    @Autowired
    private HomeworkService homeworkService;

    /**
     * 分页查询作业信息
     */
    @GetMapping
    public Result page(HomeworkQueryParam homeworkQueryParam){
        log.info("分页请求参数:{}" ,homeworkQueryParam);
        PageResult<Homework> pageResult = homeworkService.page(homeworkQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 保存作业
     */
    @Log
    @PostMapping
    public Result save(@RequestBody Homework homework){
        log.info("保存信息：{}",homework);
        homeworkService.save(homework);
        return Result.success();
    }

    /**
     * 批量删除作业
     */
    @Log
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("根据ID批量删除作业: {}",ids);
        homeworkService.delete(ids);
        return Result.success();
    }

    /**
     * 根据ID查询作业
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据ID查询作业: {}",id);
        Homework homework = homeworkService.getById(id);
        return Result.success(homework);
    }

    /**
     * 根据ID修改作业
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Homework homework){
        log.info("根据ID修改作业：{}",homework);
        homeworkService.update(homework);
        return Result.success();
    }

    /**
     * 根据课程ID查询作业
     */
    @GetMapping("/course/{courseId}")
    public Result getByCourseId(@PathVariable Integer courseId){
        log.info("根据课程ID查询作业: {}",courseId);
        List<Homework> homeworks = homeworkService.getByCourseId(courseId);
        return Result.success(homeworks);
    }

    /**
     * 根据班级ID查询作业
     */
    @GetMapping("/clazz/{clazzId}")
    public Result getByClazzId(@PathVariable Integer clazzId){
        log.info("根据班级ID查询作业: {}",clazzId);
        List<Homework> homeworks = homeworkService.getByClazzId(clazzId);
        return Result.success(homeworks);
    }

    /**
     * 根据教师ID查询作业
     */
    @GetMapping("/teacher/{teacherId}")
    public Result getByTeacherId(@PathVariable Integer teacherId){
        log.info("根据教师ID查询作业: {}",teacherId);
        List<Homework> homeworks = homeworkService.getByTeacherId(teacherId);
        return Result.success(homeworks);
    }
} 